package com.ltedu.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ltedu.common.contants.Constants;
import com.ltedu.common.enums.ErrorCodeEnum;
import com.ltedu.common.exception.BaseException;
import com.ltedu.course.domain.Course;
import com.ltedu.course.domain.Introduce;
import com.ltedu.course.domain.dto.CourseDO;
import com.ltedu.course.domain.dto.CourseInfoDO;
import com.ltedu.course.domain.vo.CourseVO;
import com.ltedu.course.mapper.CourseMapper;
import com.ltedu.course.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltedu.course.service.IntroduceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 课程表 服务实现类
 * </p>
 *
 * @author Jerry
 * @since 2020-11-26
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private IntroduceService introduceService;

    /**
     * 保存课程信息
     *
     * @param courseVo
     * @return {@link Boolean}
     */
    @Override
    public Long save(CourseVO courseVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        course.setSaleState(Constants.FROZEN);
        course.setState(Constants.NORMAL);
        course.setIsDeleted(Constants.NOT_DELETED);
        course.setGmtCreate(new Date());
        course.setGmtModified(new Date());
        int row = this.baseMapper.insert(course);
        if (row == 0) {
            throw new BaseException(ErrorCodeEnum.SAVE_COURSE_FAIL);
        }

        // 添加课程介绍
        Introduce introduce = new Introduce();
        introduce.setCourseId(course.getId());
        introduce.setGmtCreate(new Date());
        introduce.setGmtModified(new Date());
        introduce.setIntroduce(courseVo.getIntroduce());
        boolean save = introduceService.save(introduce);
        if (!save) {
            throw new BaseException(ErrorCodeEnum.SAVE_COURSE_INTRODUCE_FAIL);
        }
        return course.getId();
    }

    /**
     * 通过id获取课程信息
     *
     * @param courseId 课程id
     * @return {@link CourseDO}
     */
    @Override
    public CourseDO getCourseById(Long courseId) {
        return this.baseMapper.selectCourseById(courseId);
    }

    /**
     * 分页查询课程列表
     *
     * @param coursePage
     * @param queryWrapper
     * @return {@link IPage<CourseDO>}
     */
    @Override
    public IPage<CourseDO> coursePageList(IPage<Course> coursePage, QueryWrapper<Course> queryWrapper) {
        return this.baseMapper.selectCoursePageList(coursePage, queryWrapper);
    }

    /**
     * 根据id获取课程确认信息
     *
     * @param id id
     * @return {@link CourseInfoDO}
     */
    @Override
    public CourseInfoDO getCourseInfoById(Long id) {
        return this.baseMapper.selectCourseInfoById(id);
    }
}
